package com.rtech.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rtech.app.exception.SearchException;
import com.rtech.app.service.TraversalService;
import com.rtech.model.File;
import com.rtech.model.LoactionInfo;

/**
 * Class exposing the requested service to end user.
 * 
 * @author ruchi.singhal
 * 
 */
@Controller
public class TraversalServiceController {

	@Autowired
	TraversalService traversalService;

	/**
	 * URL for Directory Search.
	 * 
	 * @param fullPath
	 * @return
	 */
	@RequestMapping(value = "/location/dirSearch/{dirPath}", method = RequestMethod.GET)
	public @ResponseBody String directorySearch(String dirPath) {
		
		LoactionInfo response = null;
		try {
			response = traversalService.directorySearch(dirPath);
			// return new ModelAndView("jaxbView", BindingResult.MODEL_KEY_PREFIX +
			// "response", response);
			System.out.println(response+"-- directory information ");
		}catch(SearchException e) {
			// log the error information
			// set message for client side.
		}
		return "";
		
	}

	/**
	 * URL for File Serach.
	 * 
	 * @param filePath
	 * @return
	 */
	@RequestMapping(value = "/file/{filePath}")
	@ResponseBody
	public File fileSearch(@PathVariable String filePath) {
		
		System.out.println("I am here ");
		System.out.println("filePath - "+filePath);
		
		File file = null;
		
		try {
			file = traversalService.fileSearch(filePath);
			System.out.println(file+"-- File information ");
		}catch(SearchException e) {
			// log the error information
			// set message for client side.
		}
		return file;
		
	} // end of fileSearchMethod
	

} // end of class